<template>
  <div />
</template>

<script>
import Qs from 'qs'
export default {
  data () {
    return {
      ts: this.newStore()
    }
  },
  mounted () {
    const query = Qs.parse(window.location.search.substring(1)) //http://hg.eke88.com
    // eslint-disable-next-line prefer-destructuring
    console.log(config.MODE)
    const code = query.code
    if (config.MODE == 'dev') {
      //this.ceshilogin()
      this.getUserInfoByDev()
      return
    } else {
      //alert(code)
      if (code && code != '') {
        // alert(3)
        // 从微信转回，进行服务器登录
        this.doLogin(code)
        return
      } else {
        this.getWxCode()
        // this.getUserInfoByDev()
      }
    }
  },
  methods: {
    GetQueryString (name) {
      var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
      var r = window.location.search.substr(1).match(reg)
      if (r != null) return unescape(r[2])
      return null
    },
    // 模拟接口
    getWxCode () {
      //alert(4)
      if (this.$route.query.apurl) {
        window.localStorage.setItem('apurl', this.$route.query.apurl)
        window.localStorage.setItem('pagename', this.$route.query.pagename)
        window.localStorage.setItem('msgid', this.$route.query.msgid)
        window.localStorage.setItem('taskid', this.$route.query.taskid)
      }

      // var a=this.$route.query.apurl
      // var b=this.$route.query.pagename
      const wxLoginUrl =
        'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx7df2e6ad6a2a8870&redirect_uri=http://wx.jbrsys.com&response_type=code&scope=snsapi_userinfo#wechat_redirect'

      // 改成ajax试试
      let url =
        wxLoginUrl + '?timestamp=' + (new Date().getTime() + Math.random())
      // alert(url)
      window.location.href = url
    },
    doLogin (code) {
      const res = {
        code: code
      }
      let _this = this
      this.spost(this.ts, '/WeixinServlet', res).then(ts => {
        if (ts.d.res == 'success') {
          window.localStorage.setItem('userid', ts.d.userid)
          window.localStorage.setItem('openid', ts.d.openid)

          if (ts.d.userid == '' || ts.d.userid == undefined) {
            _this.$router.push({ name: 'login' })
          } else {
            const req = {
              handle: 'E299744D88ADCDF5C56442DDF419DB4E6068E69A175433DA',
              openid: window.localStorage.getItem('openid')
            }
            this.spost(this.ts, '/mainservlet', req).then(ts => {
              if (ts.d.sflag == 'success') {
                window.localStorage.setItem(
                  'userid',
                  ts.d.data.operation3[0].hash.userid
                )

                window.localStorage.setItem(
                  'deptname',
                  ts.d.data.operation3[0].hash.deptname
                )

                window.localStorage.setItem(
                  'dept',
                  ts.d.data.operation3[0].hash.dept
                )

                window.localStorage.setItem(
                  'username',
                  ts.d.data.operation3[0].hash.username
                )

                window.localStorage.setItem(
                  'pid',
                  ts.d.data.operation3[0].hash.pid
                )

                window.localStorage.setItem('token', ts.d.token)

                if (
                  window.localStorage.getItem('apurl') == undefined ||
                  window.localStorage.getItem('apurl') == 'undefined' ||
                  window.localStorage.getItem('apurl') == null ||
                  window.localStorage.getItem('apurl') == ''
                ) {
                  let routeData = this.$router.resolve({ path: '/jbrhome' })
                  window.location = routeData.href
                  // _this.$router.push('/jbrhome')
                } else {
                  let tt = window.localStorage.getItem('apurl')
                  let vv = window.localStorage.getItem('pagename')
                  let msgid = window.localStorage.getItem('msgid')
                  let taskid = window.localStorage.getItem('taskid')
                  window.localStorage.removeItem('apurl')
                  window.localStorage.removeItem('pagename')
                  window.localStorage.removeItem('msgid')
                  window.localStorage.removeItem('taskid')
                  console.log("apurl", tt);
                  _this.$router.push({
                    name: tt,
                    query: { name: vv, msgid: msgid, taskid: taskid }
                  })
                }
              }
            })
          }
          // alert(4)
          //console.log('aaaaaa', ls.getItem('tel'))
        } else {
          //this.getWxCode()

          alert('获取登录信息失败！')
        }
      })
    },
    getUserInfoByDev () {
      const req = {
        handle: 'E299744D88ADCDF5C56442DDF419DB4E33B34DAB6D91425D',
        id: '081934',
        passwords: '123456'
      }
      this.spost(this.ts, '/mainservlet', req).then(ts => {
        if (ts.d.sflag == 'success') {
          window.localStorage.setItem(
            'userid',
            ts.d.data.operation3[0].hash.userid
          )
          window.localStorage.setItem(
            'username',
            ts.d.data.operation3[0].hash.username
          )
          window.localStorage.setItem('dept', ts.d.data.operation3[0].hash.dept)
          window.localStorage.setItem(
            'deptname',
            ts.d.data.operation3[0].hash.deptname
          )
          console.log(ts.d.data.operation3)
          window.localStorage.setItem('pid', ts.d.data.operation3[0].hash.pid)
          window.localStorage.setItem('token', ts.d.token)
          this.$router.push('/jbrhome')
        }
      })
    },
    ceshilogin () {
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
</style>
